package com.google.code.netcheckin.io;

import java.io.IOException;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Arrays;

import net.sourceforge.jbird.io.StringArrayStreamWriter;

import com.google.code.netcheckin.aprs.AprsFormatter;
import com.google.code.netcheckin.aprs.Station;

/**
  * Use this class to write stations to a CSV file. 
  * <ul>
  * <li>Instantiate.</li>
  * <li>Use write methods.</li>
  * <li>close</li>
  * </ul>
  * Column names will be written as the first line. Double quotes will
  * enclose fields that contain commas.
  * <p>
  * You can use the main method to produce a file that contains a line of
  * comma-separated column names.
  * @author Dick Repasky
  *
*/

public class StationDbWriter {

	protected StringArrayStreamWriter writer;

	protected String[] buffers = 
				new String[StationDbReader.COL_NAMES.length];
	protected AprsFormatter formatter;

	public StationDbWriter(Writer w) throws IOException {
		writer = new StringArrayStreamWriter(w);
		writer.setDelimiter(Character.toString(
				(char)StationDbReader.DELIMITER));
		writer.setDelimiterQuote('"');
		writer.write(StationDbReader.COL_NAMES);
		formatter = new AprsFormatter();
	}

	public void close() throws IOException {
		writer.close();
	}

		/** Write one station. */

	public void write(final Station stn) throws IOException {
		fillBuffer(stn);
		writer.write(buffers);
	}

		/** Write a bunch of stations. */
	public void write(final Object[] stns) throws IOException {
		final int ceil = stns.length;
		for (int idx = 0; idx < ceil; idx ++) {
			write((Station)stns[idx]);
		}
	}

		/**
		  * Use this method to write a file that contains
		  * a line of comma-separated column names.
		  * If no argument is given, the file will be "template.csv".
		  * Otherwise, the file name will be the first argument.
		  *
		*/

	public static void main(String[] argv) throws Exception {
		String destname = "template.csv";
		if (argv != null && argv.length > 0) {
			destname = argv[0];
		}
		StationDbWriter sdbw = new StationDbWriter(
					new FileWriter(destname));
		sdbw.close();
	}

	////////////////////// protected below //////////////////////////

		/**
		  * Fill array of strings to be position, one array element
		  * per field in the Station object.
		  *
		*/

	protected void fillBuffer(Station stn) {
		Arrays.fill(buffers, "");
		String hold = stn.getCallsign();
		if (hold != null) {
			buffers[StationDbReader.IDX_CALL_SGN] = hold;
		}
		hold = stn.getName();
		if (hold != null) {
			buffers[StationDbReader.IDX_NAME] = hold;
		}
		buffers[StationDbReader.IDX_ARES] = stn.getIsAres() 
				? "yes" : "no";
		buffers[StationDbReader.IDX_RACES] = stn.getIsRaces() 
				? "yes" : "no";
		hold = stn.getCellphone();
		if (hold != null) {
			buffers[StationDbReader.IDX_CELLPHONE] = hold;
		}
		hold = stn.getEmail();
		if (hold != null) {
			buffers[StationDbReader.IDX_EMAIL] = hold;
		}
			// primary location
		hold = stn.getPrimaryDescription();
		if (hold != null) {
			buffers[StationDbReader.IDX_PRIM_DESCR] = hold;
		}
		hold = stn.getPrimaryAddress();
		if (hold != null) {
			buffers[StationDbReader.IDX_PRIM_ADDR] = hold;
		}
		hold = stn.getPrimaryCity();
		if (hold != null) {
			buffers[StationDbReader.IDX_PRIM_CITY] = hold;
		}
		hold = stn.getPrimaryState();
		if (hold != null) {
			buffers[StationDbReader.IDX_PRIM_STATE] = hold;
		}
		hold = stn.getPrimaryZip();
		if (hold != null) {
			buffers[StationDbReader.IDX_PRIM_ZIP] = hold;
		}
		int degs = stn.getPrimaryLatitude1();
		if (degs != Station.MISSING_INT) {
			buffers[StationDbReader.IDX_PRIM_LAT_DEG] = 
				formatter.formatDoubleZero(degs);
		}
		float mins = stn.getPrimaryLatitude2();
		if (mins != Station.MISSING_FLOAT) {
			buffers[StationDbReader.IDX_PRIM_LAT_MIN] = 
				formatter.formatCoordinateMinutes(mins);
		}
		char dir = stn.getPrimaryLatitudeOrientation();
		if (dir != Station.MISSING_CHAR) {
			buffers[StationDbReader.IDX_PRIM_LAT_DIR] = 
				Character.toString(dir);
		}
		degs = stn.getPrimaryLongitude1();
		if (degs != Station.MISSING_INT) {
			buffers[StationDbReader.IDX_PRIM_LONG_DEG] = 
				formatter.formatDoubleZero(degs);
		}
		mins = stn.getPrimaryLongitude2();
		if (mins != Station.MISSING_FLOAT) {
			buffers[StationDbReader.IDX_PRIM_LONG_MIN] = 
				formatter.formatCoordinateMinutes(mins);
		}
		dir = stn.getPrimaryLongitudeOrientation();
		if (dir != Station.MISSING_CHAR) {
			buffers[StationDbReader.IDX_PRIM_LONG_DIR] = 
				Character.toString(dir);
		}
		hold = stn.getPrimaryIcon();
		if (hold != null) {
			buffers[StationDbReader.IDX_PRIM_ICON] = hold;
		}
			// secondary locaion
		hold = stn.getSecondaryDescription();
		if (hold != null) {
			buffers[StationDbReader.IDX_SEC_DESCR] = hold;
		}
		hold = stn.getSecondaryAddress();
		if (hold != null) {
			buffers[StationDbReader.IDX_SEC_ADDR] = hold;
		}
		hold = stn.getSecondaryCity();
		if (hold != null) {
			buffers[StationDbReader.IDX_SEC_CITY] = hold;
		}
		hold = stn.getSecondaryState();
		if (hold != null) {
			buffers[StationDbReader.IDX_SEC_STATE] = hold;
		}
		hold = stn.getSecondaryZip();
		if (hold != null) {
			buffers[StationDbReader.IDX_SEC_ZIP] = hold;
		}
		degs = stn.getSecondaryLatitude1();
		if (degs != Station.MISSING_INT) {
			buffers[StationDbReader.IDX_SEC_LAT_DEG] = 
				formatter.formatDoubleZero(degs);
		}
		mins = stn.getSecondaryLatitude2();
		if (mins != Station.MISSING_FLOAT) {
			buffers[StationDbReader.IDX_SEC_LAT_MIN] = 
				formatter.formatCoordinateMinutes(mins);
		}
		dir = stn.getSecondaryLatitudeOrientation();
		if (dir != Station.MISSING_CHAR) {
			buffers[StationDbReader.IDX_SEC_LAT_DIR] = 
				Character.toString(dir);
		}
		degs = stn.getSecondaryLongitude1();
		if (degs != Station.MISSING_INT) {
			buffers[StationDbReader.IDX_SEC_LONG_DEG] = 
				formatter.formatDoubleZero(degs);
		}
		mins = stn.getSecondaryLongitude2();
		if (mins != Station.MISSING_FLOAT) {
			buffers[StationDbReader.IDX_SEC_LONG_MIN] = 
				formatter.formatCoordinateMinutes(mins);
		}
		dir = stn.getSecondaryLongitudeOrientation();
		if (dir != Station.MISSING_CHAR) {
			buffers[StationDbReader.IDX_SEC_LONG_DIR] = 
				Character.toString(dir);
		}
		hold = stn.getSecondaryIcon();
		if (hold != null) {
			buffers[StationDbReader.IDX_SEC_ICON] = hold;
		}
		hold = stn.getComment();
		if (hold != null) {
			buffers[StationDbReader.IDX_COMMENT] = hold;
		}
	}

}
